package com.recursivechaos.gamemaster.dao;

/**
 * WEAPON DAO
 * Handles all weapon activities/functions
 * @author Andrew Bell
 */
import java.util.List;
import java.util.Random;

import org.hibernate.HibernateException;

import com.recursivechaos.gamemaster.classes.WeaponObj;
import com.recursivechaos.gamemaster.exception.GameException;

public class WeaponObjDAO extends DAO {
	/**
	 * Given the exact string name of a weapon, returns the weapon object
	 * 
	 * @param weapString
	 *            Exact string of weapon
	 * @return Weapon object matching the string
	 * @throws GameException
	 *             Error loading all the data from string
	 *             
	 */
	private WeaponObj findWeapInExactString(String weapString)
			throws GameException {
		WeaponObj foundWeap;
		try {
			begin();
			foundWeap = (WeaponObj) getSession().get(WeaponObj.class,
					weapString);
			commit();
		} catch (HibernateException e) {
			rollback();
			throw new GameException("Could not get weapon details.");
		} finally {
			close();
		}
		return foundWeap;
	}

	/**
	 * Returns weapon object found anywhere in string
	 * 
	 * @param string
	 *            String to check for weapon
	 * @return WeaponObj
	 * @throws GameException
	 *             Either too many, or not enough weapons found in string
	 */
	public WeaponObj findWeaponInString(String string) throws GameException {
		WeaponObj foundWeap;
		// Find if string contains a weapon
		String weapString = "";
		List<String> weapList = getWeaponStringList();
		for (int i = 0; i < weapList.size(); i++) {
			String rname = weapList.get(i);
			if (string.contains(rname.toUpperCase())) {
				if (weapString.equals("")) {
					weapString = weapList.get(i);
				} else {
					throw new GameException("Dual weilding not allowed.");
				}
			}
		}
		if (weapString.equals("")) {
			throw new GameException("No weapon found.");
		}
		// Once we've checked the input for a weapon, we get the object
		foundWeap = findWeapInExactString(weapString);
		return foundWeap;
	}

	/**
	 * Get a random weapon
	 * 
	 * @return Weapon obj of random weapon
	 * @throws GameException
	 */
	public WeaponObj getRandomWeapon() throws GameException {
		List<String> weapList = getWeaponStringList();
		Random generator = new Random();
		int r = generator.nextInt(weapList.size());
		String randWeap = weapList.get(r);
        return findWeapInExactString(randWeap);
	}

	/**
	 * Get a List<String> of available weapons
	 * 
	 * @return List<String> of weapons
	 * @throws GameException
	 */
	@SuppressWarnings({ "unchecked" })
	public List<String> getWeaponStringList() throws GameException {
		List<String> weapList;
		try {
			begin();
			weapList = getSession().createSQLQuery(
					"SELECT WEAP_TYPE FROM recursi3_igm_db.weaponobjs;").list();
			commit();
		} catch (Exception e) {
			rollback();
			throw new GameException("Error fetching weapons.");
		} finally {
			close();
		}

		return weapList;
	}
}
